package com.link2room.bumblebee.facade.impl;


import com.link2room.bumblebee.facade.ISysOptionFacadeService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.SysOptionBaseParamDto;
import com.link2room.common.dto.result.base.SysOptionDto;
import com.link2room.common.entity.SysOptions;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by LN on 2016/12/20.
 */
@Component("sysOptionFacadeService")
public class SysOptionFacadeServiceImpl implements ISysOptionFacadeService {
    @Resource
    private ICommonService commonService;
    @Override
    public SysOptionDto findSysOption(SysOptionBaseParamDto paramDto) throws Exception {
        String hotelGroupCode = paramDto.getLhotelGroupCode();
        if(StringUtil.isBlank(hotelGroupCode)){
            hotelGroupCode = DefaultValue.LHOTEL_GROUP_CODE;
        }
        SysOptions find = new SysOptions();
        find.setLhotelGroupCode(hotelGroupCode);
        find.setItem(paramDto.getItem());
        find.setCategory(paramDto.getCategory());
        SysOptions opt = commonService.findOneEQ(SysOptions.class, find);
        if(opt == null){
            throw new SysException(
                    BaseSysExceptionEnum.SYS_OPTION.getCode(),
                    BaseSysExceptionEnum.SYS_OPTION.getMsg()+"[lhotelGroupCode] :"+paramDto.getLhotelGroupCode()+" "+paramDto.toString(),
                    BaseSysExceptionEnum.SYS_OPTION.getBizType());
        }
        return ClassConverUtil.copyProperties(opt,SysOptionDto.class);
    }

    @Override
    public QueryResult<SysOptionDto> findSysOptions(SysOptionBaseParamDto paramDto) throws Exception {
        String hotelGroupCode = paramDto.getLhotelGroupCode();
        if(StringUtil.isBlank(hotelGroupCode)){
            hotelGroupCode = DefaultValue.LHOTEL_GROUP_CODE;
        }
        SysOptions find = new SysOptions();
        find.setLhotelGroupCode(hotelGroupCode);
        find.setItem(paramDto.getItem());
        find.setCategory(paramDto.getCategory());
        QueryResult<SysOptions> qre = commonService.findAllEQ(SysOptions.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qre,SysOptionDto.class,true);
    }
}
